(function() {
    // getting a reference to the main namespace if it exists - empty object otherwise
    var Prestatheme = window.Prestatheme = window.Prestatheme || {};
    
    /*
     * Fonction d'initialisation des variables globales
     */
    Prestatheme.onChangeColor = function(selector) 
    {
        Prestatheme.runDebug ("[LIVE-UPDATES][onChangeColor] A color style is changed");
        try 
        {
            var valueId     = $(selector).attr('id');
            var baseFieldId = valueId.replace('value_', '');
			
            var value          = $("input[id='"+ valueId +"']").val();
            var targetSelector = $("input[id='selector_"+ baseFieldId +"']").val();
            var fullValue      = $("input[id='fullvalue_"+ baseFieldId +"']").val();
            var attribute      = $("input[id='attr_"+ baseFieldId +"']").val();
            
            //apply the style to the view page
			var stylecss = fullValue.split(';');
			for (var i=0; i<stylecss.length; i++)
			{
				style = stylecss[i];
				style = style.split(':');
				
				if (style.length != 2) break;
				
				Prestatheme.applyChanges(targetSelector, style[0], style[1]);
			}
            Prestatheme.applyChanges(targetSelector, attribute, value);
			Prestatheme.updated = true;
        }
        catch (e)
        {
            Prestatheme.manageError ('OCC', e);
        }
    };	
	
    Prestatheme.onChooseDesign = function(selector) 
    {
        Prestatheme.runDebug ("[LIVE-UPDATES][onChooseDesign] A design style has been choosen");
        try 
        {
            var classChoosen = $(selector).attr('value');
            var elemName     = $(selector).attr('name');
			var targetElem   = $('#'+ elemName).val();
			
			Prestatheme.updateDesign(elemName, targetElem, classChoosen);
			Prestatheme.applyDesign(elemName, targetElem, classChoosen);
			
			Prestatheme.updated = true;
        }
        catch (e)
        {
            Prestatheme.manageError ('OCC', e);
        }
    };
    
    Prestatheme.applyChanges = function (select, attr, val) 
    {
		Prestatheme.runDebug ("[LIVE-UPDATES][applyChanges] Apply changes");
        try 
        {
            if (attr && val)
            {
				// Apply changes on the target elements
				Prestatheme.updateStyle(select, attr, val);
				Prestatheme.applyStyle(select, attr, val);
            }
        }
        catch (e)
        {
            Prestatheme.manageError ('ACF', e);
        }
    };
	
    Prestatheme.updateStyle = function(select, attr, val) 
    {
        try
        {
            Prestatheme.runDebug ("[LIVE-UPDATES][updateStyle] Add or update a css style");
			
			if (Prestatheme.styles == undefined)
				Prestatheme.styles = new Array();
            
			var item, updated = false, stylesToString = '[';
			for (var i=0; i<Prestatheme.styles.length; i++)
			{
				item = Prestatheme.styles[i];
				if (item.selector == select && item.attribute == attr)
				{
					updated = true;
					item.value = val;
					Prestatheme.styles[i] = item;
					item = Prestatheme.styles[i];
				}
				stylesToString += '{"selector":"'+ item.selector +'","attribute":"'+ item.attribute +'","value":"'+ item.value +'"},';
			}
			if (stylesToString != '[')
				stylesToString = stylesToString.substring(0, stylesToString.length - 1);
			if (!updated)
			{
				if (stylesToString != '[')
					stylesToString += ',';
				stylesToString += '{"selector":"'+ select +'","attribute":"'+ attr +'","value":"'+ val +'"}';
			}
			stylesToString += ']';
			Prestatheme.styles = $.parseJSON(stylesToString, false);
        }
        catch (e)
        {
            Prestatheme.manageError ('UDS', e);
        }
    };
	
    Prestatheme.updateDesign = function (elemName, targetElem, classChoosen)
    {
        try
        {
            Prestatheme.runDebug ("[LIVE-UPDATES][updateDesign] Add or update a design");
			
			if (Prestatheme.designCfg == undefined)
				Prestatheme.designCfg = new Array();
			
			var item, updated = false, designToString = '[';
			for (var i=0; i<Prestatheme.designCfg.length; i++)
			{
				item = Prestatheme.designCfg[i];
				if (item.elemName == elemName && item.targetElem == targetElem)
				{
					updated = true;
					item.classChoosen = classChoosen;
					Prestatheme.designCfg[i] = item;
					item = Prestatheme.designCfg[i];
				}
				designToString += '{"elemName":"'+ item.elemName +'","targetElem":"'+ item.targetElem +'","classChoosen":"'+ item.classChoosen +'"},';
			}
			if (designToString != '[')
				designToString = designToString.substring(0, designToString.length - 1);
			if (!updated)
			{
				if (designToString != '[')
					designToString += ',';
				designToString += '{"elemName":"'+ elemName +'","targetElem":"'+ targetElem +'","classChoosen":"'+ classChoosen +'"}';
			}
			designToString += ']';
			Prestatheme.designCfg = $.parseJSON(designToString, false);
        }
        catch (e)
        {
            Prestatheme.manageError ('UDD', e);
        }
    };
})();